package com.example.beikeyigou.login;

import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.beikeyigou.R;
import com.example.beikeyigou.untils.BaseFragment;
import com.example.beikeyigou.untils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;


public class RegisterFragment extends BaseFragment {

    EditText  telEt,nicknameEt,pwdEt,repwdEt,questionEt,answerEt;
    Button ensureBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_register, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        telEt = view.findViewById(R.id.frag_reg_et_tel);
        nicknameEt = view.findViewById(R.id.frag_reg_et_name);
        pwdEt = view.findViewById(R.id.frag_reg_et_pwd);
        repwdEt = view.findViewById(R.id.frag_reg_et_repwd);
        ensureBtn = view.findViewById(R.id.frag_reg_btn);
        questionEt = view.findViewById(R.id.frag_reg_et_question);
        answerEt = view.findViewById(R.id.frag_reg_et_answer);

        ensureBtn.setOnClickListener(listener);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String tel = telEt.getText().toString();
            String nickName = nicknameEt.getText().toString();
            String pwd = pwdEt.getText().toString();
            String rePwd = repwdEt.getText().toString();
            String question = questionEt.getText().toString();
            String answer = answerEt.getText().toString();

            boolean flag = judgeInput(tel,nickName,pwd,rePwd,question,answer);


            if (flag) {

                Map<String,String> map = new HashMap<>();
                map.put("phone",tel);
                map.put("password",pwd);
                map.put("name",nickName);
                map.put("address","");
                map.put("nickname",nickName);
                map.put("question",question);
                map.put("answer",answer);

                postNetWork(URLUtils.register_url,map);

            }
        }
    };

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        UserBean userBean = new Gson().fromJson(result,UserBean.class);
        int code = userBean.getCode();
        switch (code){
            case 200://注册成功
                Toast.makeText(getContext(),"注册成功！",Toast.LENGTH_SHORT).show();
                break;
            case 201 :
                Toast.makeText(getContext(),"手机号语句被注册，请直接登录",Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getContext(),"注册失败！",Toast.LENGTH_SHORT).show();
                break;


        }

    }

    //判断输入的内容是否符合要求
    private boolean judgeInput(String tel,String nickName,String pwd,String rePwd,String question,String answer) {

        if (TextUtils.isEmpty(tel) || TextUtils.isEmpty(nickName) ||TextUtils.isEmpty(pwd) ||TextUtils.isEmpty(rePwd)
        ||TextUtils.isEmpty(question) || TextUtils.isEmpty(answer)
        ){

            Toast.makeText(getContext(),"输入内容不可为空",Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!rePwd.equals(pwd)){
            Toast.makeText(getContext(),"两次输入的密码不同",Toast.LENGTH_SHORT).show();
            return false;
        }

        //判断输入的电话号码是否满足号码格式
        String regex = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";
        if (!tel.matches(regex)){
            Toast.makeText(getContext(),"输入的电话号码不合法",Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;

    }

}